$(document).ready(function(){
	
	//==============jqgrid===============
	jQuery("#boxIdList").jqGrid( {
		//url : 'dump/searchBoxId.do?id=-1',
		datatype : "local",
		//mtype : 'POST',
		colNames : ['箱号','型号','订单','规格模数','玻璃数量','完成状态','客户代码'],
		colModel : [
		    {name : 'boxId',index : 'boxId',width : 50,align : "center",sortable : true},
		    {name : 'mdl',index : 'mdl',width : 50,align : "center",sortable : true},
			{name : 'internal',index : 'internal',width : 30,align : "center",sortable : true},
			{name : 'spec',index : 'spec',width : 30,align : "center",sortable : true},
			{name : 'qty',index : 'qty',width : 20,align : "center",sortable : true},
			{name : 'stat',index : 'stat',width : 20,align : "center",sortable : true},
			{name : 'cus',index : 'cus',width : 20,align : "center",sortable : true}
		],
		rowNum : 15,
		sortname : 'boxId',
		viewrecords : true,
		sortorder : "desc",
		recordtext: "当前{0} - {1}，共 {2}",  
		emptyrecords: "未查询到数据",  
		loadtext: "读取中...",  
		pgtext : "当前 {0} 共 {1} 页" , 
		pager : jQuery('#boxIdList_pager'),
		altclass : 'altRowsColour',
		//width : $("#boxIdList").parent("div").width(),
		//height : $(window).height()*0.6,
		caption : "箱子",
		multiselect : true,
		rownumbers : true
		
	}).navGrid('#boxIdList_pager', {
		add : false,
		edit : false,
		del : false,
		search : false,
		refresh : false
	});
	
	//==================================
	
	function setGridInfo(data,smpGrid) {
		$(smpGrid).jqGrid("clearGridData");
	  	if (data != null && data != undefined) {
			  $(smpGrid).jqGrid('setGridParam', {
				datatype : 'local',
				data : data
			  }).trigger("reloadGrid");
	  	} else {
	        $(smpGrid).jqGrid("addRowData", "1", data);
	     }
	};
	
	function listTab(boxId,internal,cust){
		$.ajax({
			type:"post",
			url : "dump/searchBoxId.do",
			data : {"id" : boxId, "internal" : internal, "cust" : cust},
			ansyc:false,
			success:function(msg){
				setGridInfo(msg.grid,$("#boxIdList"));
			}
		});
	}
	
	
	listTab("","","");
	
	//==================================
	
	//input 事件
	//设置获取焦点
	$("#edi_boxIdFk").focus();
	
	//转成大写
	$("#edi_boxIdFk,#edi_internal,#edi_cust").unbind('keyup').bind('keyup',function(event){
		var val = $(this).val();
		if(event.keyCode == 13){
			$("#edi_searchBtn").click();
		}else{
			val = val.toUpperCase();
			$(this).val(val);
		}
	});
	$("#a").click(function(){
		alert('a');
	});
	$("#edi_searchBtn").click(function(){
		var boxId = $("#edi_boxIdFk").val();
		var internal = $("#edi_internal").val();
		var cust = $("#edi_cust").val();
		listTab(boxId,internal,cust);
	});
	
	//模式一
	$("#datDumpMoBtn").click(function(){
		var rowIds = $("#boxIdList").getGridParam("selarrrow");
		if(!rowIds.length > 0){
			promptDialog_Show("提示","请先勾选需要导出的数据。");
			return false;
		}
		
		var selRows = [];
		var html = "";
		for(var i = 0; i < rowIds.length; i ++){
			var selRow = $("#boxIdList").jqGrid('getRowData',rowIds[i]);
			if(selRow.cus == "088" || 
					selRow.cus == "071" || 
					selRow.cus == "168"|| 
					selRow.cus == "169"){
				promptDialog_Show("提示","箱号为：" + selRow.boxId + " 的箱子请使用模式三导出。");
				return false;
			}
			selRows.push(selRow.boxId);
			html += "<tr class='Dia_export_tr unCreated' var='" + selRow.boxId + "'>" +
				"<td class='Dia_export_boxId' " +
				"var='" + selRow.boxId + "@" + selRow.cus + "'>" + 
				selRow.boxId + "</td>" +
				"<td class='Dia_export_internal' " +
				"var='" + selRow.internal + "'>" + 
				selRow.internal + "</td>" +
				"<td class='Dia_export_state' " +
				"var='" + selRow.boxId + "'>" + 
				"等待中</td>" +
				"<td class='hide'><input class='Dia_export_fileName' " +
				"var='" + selRow.boxId + "' /></td>" +
				"</tr>";
		}
		openDownloadDlg(html, "1", "模式一：给FDT，MLCM及WJ格式");
	});
	
	
	//模式二
	$("#datDumpMtBtn").click(function(){
		var rowIds = $("#boxIdList").getGridParam("selarrrow");
		if(!rowIds.length > 0){
			promptDialog_Show("提示","请先勾选需要导出的数据。");
			return false;
		}
		
		var selRows = [];
		var html = "";
		for(var i = 0; i < rowIds.length; i ++){
			var selRow = $("#boxIdList").jqGrid('getRowData',rowIds[i]);
			if(selRow.cus == "088" ||
					selRow.cus == "071"|| 
					selRow.cus == "168"|| 
					selRow.cus == "169"){
				promptDialog_Show("提示","箱号为：" + selRow.boxId + " 的箱子请使用模式三导出。");
				return false;
			}
			selRows.push(selRow.boxId);
			html += "<tr class='Dia_export_tr unCreated' var='" + selRow.boxId + "'>" +
				"<td class='Dia_export_boxId' " +
				"var='" + selRow.boxId + "@" + selRow.cus + "'>" + 
				selRow.boxId + "</td>" +
				"<td class='Dia_export_internal' " +
				"var='" + selRow.internal + "'>" + 
				selRow.internal + "</td>" +
				"<td class='Dia_export_state' " +
				"var='" + selRow.boxId + "'>" + 
				"等待中</td>" +
				"<td class='hide'><input class='Dia_export_fileName' " +
				"var='" + selRow.boxId + "' /></td>" +
				"</tr>";
		}
		openDownloadDlg(html, "2", "模式二：回CPT-L厂");
	});
	
	//模式三
	$("#datDumpMthBtn").click(function(){
		var rowIds = $("#boxIdList").getGridParam("selarrrow");
		if(!rowIds.length > 0){
			promptDialog_Show("提示","请先勾选需要导出的数据。");
			return false;
		}
		
		var selRows = [];
		var html = "";
		for(var i = 0; i < rowIds.length; i ++){
			var selRow = $("#boxIdList").jqGrid('getRowData',rowIds[i]);
			if(selRow.cus != "088" &&
					selRow.cus != "071"&&
					selRow.cus != "168"&&
					selRow.cus != "169"){
				promptDialog_Show("提示","箱号为：" + selRow.boxId + " 的箱子请使用其他模式导出。");
				return false;
			}
			/*else if(selRow.stat != "SHTC"){
				promptDialog_Show("提示","箱号为：" + selRow.boxId + " 的箱子不是出货状态。");
				return false;
			}*/
			
			selRows.push(selRow.boxId);
			html += "<tr class='Dia_export_tr unCreated' var='" + selRow.boxId + "'>" +
			"<td class='Dia_export_boxId' " +
			"var='" + selRow.boxId + "@" + selRow.cus + "'>" + 
			selRow.boxId + "</td>" +
			"<td class='Dia_export_internal' " +
			"var='" + selRow.internal + "'>" + 
			selRow.internal + "</td>" +
			"<td class='Dia_export_state' " +
			"var='" + selRow.boxId + "'>" + 
			"等待中</td>" +
			"<td class='hide'><input class='Dia_export_fileName' " +
			"var='" + selRow.boxId + "' /></td>" +
			"</tr>";
		}
		openDownloadDlg(html, "3", "模式三：天马XML格式");
	});
	
	//图片
	$("#datDumpMoBtn").popover({
		html 		: true,
		placement 	: "bottom",
		trigger		: "hover",
		title		: $("#datDumpMo_etc_title").html(),
		content		: $("#datDumpMo_etc").html()
	});
	
	$("#datDumpMtBtn").popover({
		html 		: true,
		placement 	: "bottom",
		trigger		: "hover",
		title		: $("#datDumpMt_etc_title").html(),
		content		: $("#datDumpMt_etc").html()
	});
	
	$("#datDumpMthBtn").popover({
		html 		: true,
		placement 	: "bottom",
		trigger		: "hover",
		title		: $("#datDumpMth_etc_title").html(),
		content		: $("#datDumpMth_etc").html()
	});
	
	
	//dialog的导出按钮
	$("#Dia_export_confirm").click(function(){
		var type = $("#Dia_export_type").val();
		$(".Dia_export_boxId").each(function(){
			var info_ary = $(this).attr("var").split("@");
			var id = info_ary[0];
			var cusId = info_ary[1];
			$(".Dia_export_state[var='" + id + "']").html("生成中");
			var flag = createFile(id,cusId,$('input[name="Dia_export_ifTrial"]:checked').val(),type);
			if(flag){
				$(".Dia_export_tr[var='" + id + "']").removeClass("unCreated");
			}
		});
		
		//文件生成完毕后交给后台打包
		var items = 0;
		$(".Dia_export_tr").each(function(){
			if(!$(this).hasClass("unCreated")){
				$("#downloadForm").append("<input type='input' name='boxIds' value='" + $(this).find(".Dia_export_fileName").val() + "' />");
				items++;
			}
		});
		if(items <= 0)
			return false;
		var name = "文件下载";
		$("#downloadForm").attr("target",name);
		$("#downloadSub").click();
		$("#downloadForm").find("input[type='input']").remove();
		
	});
	
	//生成bat或者xml文件
	function createFile(id,cusId,iftrial,type){
		var str = "";
		$.ajax({
			type	:"post",
			url		:"dump/createFile.do",
			data	:{id : id, cust : cusId, iftrial : iftrial, type: type},
			async	: false,
			success : function(msg){
				str = msg.msg;
				if(str == "success"){
					$(".Dia_export_state[var='" + id + "']").html("生成成功");
					$(".Dia_export_fileName[var='" + id + "']").val(msg.name);
				}else{
					$(".Dia_export_state[var='" + id + "']").html("生成失败，请检查数据");
				}
			}
		});
		if(str == "success")
			return true;
		return false;
	}
	
	//打开下载dialog
	function openDownloadDlg(html,type,title){
		
		$("#Dia_export_table").html(html);
		$("#Dia_exprot_ifTrialDiv").addClass("hide");
		if(type == "1")
			$("#Dia_exprot_ifTrialDiv").removeClass("hide");
		$("#Dia_export_type").val(type);
		$("#Dia_exportHeader").html(title);
		$("#Dia_export").modal({backdrop:false},"open");
	}
	
	/** 弹出提示dialog */
	var times = null;
	function promptDialog_Show(head,html){
		$("#Dia_promptHeader").html(head);
		$("#Dia_promptTxt").html(html);
		$("#Dia_prompt").modal({backdrop:false});
		
		clearTimeout(times);
		function promptDialog_Hide(){$("#Dia_prompt").modal("hide");}
		times = setTimeout(promptDialog_Hide,"2500");
	}
	
	
	function resetGrid(){
		var offsetBottom, divWidth;
		divWidth = $("#boxIdTableDiv").width();
		offsetBottom = $(window).height() - $("#boxIdTableDiv").offset().top;
		$("#boxIdTableDiv").height(offsetBottom);
		
		$("#boxIdList").setGridWidth(divWidth);
		$("#boxIdList").setGridHeight(offsetBottom * 0.95 - 82);
		
		//$(window).unbind("onresize"); 
		//$("#A110_List").setGridWidth($("#A110_ListDiv").width()*0.95); 
		//$("#boxIdList").setGridHeight($(window).height()*0.6); 
		//$(window).bind("onresize", this);  
	}
	
	
	function init(){
		//$(window).bind("onresize",resetGrid());
		//$(window).resize(resetGrid());
		resetGrid();
		$(window).resize(function(){resetGrid();});
		$("#boxIdList_pager").css({"height":"30px"});
		$(".ui-pg-selbox").css({"width":"60px","height":"22px"});
		$(".ui-pg-input").css({"width":"20px"});
		$("#boxIdList_pager_left").css({"width":"20%"});
		$("#boxIdList_pager_center").css({"width":"40%"});
		//$(window).resize();
	}
	init();
	
});